var myOrder = angular.module("myOrderApp", ["scService"]);
myOrder.controller("myOrderController", function($scope,$rootScope, $http, scCodeService, $location, $stateParams, commonService, jsonToStr) {
    $scope.processingList = [{
        "orderNo": '',
        "createTime": "2016-07-12 18:23:55",
        "status": "预约成功",
        "statusColor":"0",
        "btn":"取消预约",
        "link": "link",
        "address": "成都市高新区天府大道中段吉泰路88"
    }, {
        "orderNo": '',
        "createTime": "2016-07-16 16:43:32",
        "status": "正在充电",
        "statusColor":"1",
        "btn":"结束充电",
        "link": "link",
        "address": "成都市高新区天府大道中段吉泰路88"
    }];
    $scope.pendingList = [{
        "orderNo": '02321985642',
        "createTime": "2016-07-12 18:23:55",
        "status": "",
        "link": "link",
        "address": "成都市高新区天府大道中段吉泰路88",
        "price": '63.16'
    }];
    $scope.overList = [{
        "orderNo": '02321985642',
        "createTime": "2016-07-12 18:23:55",
        "status": "",
        "link": "link",
        "address": "成都市高新区天府大道中段吉泰路88",
        "price": '63.16'
    }];
    // 获取正在进行中的业务
    $scope.getOngoingOperation = function(){
        var postData = {
            // userCard = localStorage.getItem("userCard");
        };
        scCodeService.getOngoingOperation(postData).then(function(data){
            if(data.code==200){
                angular.forEach(data, function(value, index){
                    // if(value.status==)
                });
            }
        });
    };

    //进行新数据组装到老数据上
    $scope.dataToOrderList = function(obj) {
            // $scope.chargelen = $scope.orderList.length;
            $scope.appserver = 'http://' + window.location.host + '/appserver/static/wechat-ht/index.html#';
            angular.forEach(obj, function(data, index, array) {
                if (array[index].status == 0) {
                    var status = "预约成功";
                    var link = $scope.appserver + '/orderPay/' + array[index].orderNo;
                    $scope.processingList.push({
                        "orderNo": array[index].orderNo,
                        "createTime": array[index].createTime,
                        "status": status,
                        "link": link
                    });
                } else if (array[index].status == 1) {
                    var status = "未支付";
                    var link = $scope.appserver + '/orderDetail/' + array[index].orderNo;
                    $scope.pendingList.push({
                        "orderNo": array[index].orderNo,
                        "createTime": array[index].createTime,
                        "caculatedTotalPrice": array[index].caculatedTotalPrice,
                        "status": status,
                        "link": link
                    });
                } else {
                    var status = "已结束";
                    var link = $scope.appserver + '/orderDetail/' + array[index].orderNo;
                    $scope.overList.push({
                        "orderNo": array[index].orderNo,
                        "createTime": array[index].createTime,
                        "caculatedTotalPrice": array[index].caculatedTotalPrice,
                        "status": status,
                        "link": link
                    });
                }
                /*$scope.orderList[$scope.chargelen] = {
                    "orderNo": array[index].orderNo,
                    "createTime": array[index].createTime,
                    "caculatedTotalPrice": array[index].caculatedTotalPrice,
                    "status": $scope.status,
                    "link": $scope.link
                };
                $scope.chargelen++;*/

            });
        }
        //定义获取订单参数
    $scope.orderList = [];
    $scope.pageNo = 1; //页码
    $scope.isPush = true; //是否需要加载数据
    $scope.hasOrder = false; //是否存在订单
    //根据用户id获取订单列表
    $scope.getOrderList = function() {
        var userId = localStorage.getItem("userId");
        var postData = {
            'userId': userId, //userId
            'pageNo': $scope.pageNo
        }
        scCodeService.getOrderList(postData).then(function(data) {
            if (data.code == "200") {
                if (data.data != "" && data.data.length > 0 && data.data != null) {
                    console.log(data.data);
                    //将新数据组装到之前数据上进行显示
                    $scope.dataToOrderList(data.data);
                    //$scope.orderList=data.data;
                    if (data.data.length < 20) {
                        $scope.isPush = false;
                    }
                } else {
                    $scope.isPush = false;
                }
                if ($scope.pageNo == 1 && data.data == "" || data.data.length == 0 || data.data == null) {
                    $scope.hasOrder = true;
                }
            }
        })
    }
    // $scope.getOrderList();
    //上拉加载数据
    window.onscroll = function() {
        // console.log("上拉了window");
        var wh = window.pageYOffset + window.screen.height;
        var dh = document.body.scrollHeight;
        // console.log("windowheight:"+window.screen.height+','+window.pageYOffset+',documentheight:'+dh);
        if (wh >= dh && $scope.isPush) {
            console.log("上拉加载数据了。。");
            $scope.pageNo++;
            $scope.getOrderList();
        }
    };

    //取消预约或结束充电
    $scope.process = function(data){
        if(data.status == 0){
            $scope.weirdConfirmSet({
                title: "",
                content: "确定要取消预约？",
                buttonAux: '点错了',
                buttonMain: '确定',
                data:{type:"1", zhangdanliushuihao:"12344462"}//type: "1"，取消预约 "2"，结束充电
            });
        }else if(data.status==1){
            $scope.weirdConfirmSet({
                title: "",
                content: "确定要结束充电？",
                buttonAux: '点错了',
                buttonMain: '确定',
                data:{type:"2", zhangdanliushuihao:"12344462"}
            });
        };
    };
    // confirm确定
    $rootScope.confirmSure=function(data){
        if(data.type==1){
            // 取消预约
            alert("已取消充电");
        }else if(data.type==2){
            // 结束充电
            alert("已结束充电");
        }
    };
});
